{extend name="base"/}
{block name="resources"}
<style type="text/css">
	.ns-form {margin-top: 0;}
	.tree-line{padding:10px 0;background:#ededed;margin-bottom:2px;line-height: 1.8;}
	.tree-line .layui-form{padding-left: 10px !important;}
	.tree-line .layui-form-checkbox{margin:0 10px !important;vertical-align:middle;}
	.ns-form {margin-top: 0;}
	.ns-item-block-hover-a {cursor: pointer;}
	.ns-input-box {position: absolute; right: 0; top: 50%; transform: translateY(-50%);}
	.layui-form-item .layui-form-checkbox[lay-skin=primary] {margin-top: 0;}
</style>
{/block}
{block name="main"}
<div class="layui-form ns-form" >
	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>用户名：</label>
		<div class="layui-input-block">
			<input name="username" type="text" autocomplete="off" readonly onfocus="this.removeAttribute('readonly');" onblur="this.setAttribute('readonly',true);" lay-verify="required" class="layui-input ns-len-long">
		</div>
		<div class="ns-word-aux">请输入登录用户名</div>
	</div>
	
	<div class="layui-form-item">
		<label class="layui-form-label">手机号：</label>
		<div class="layui-input-block">
			<input name="mobile" type="text" autocomplete="off" lay-verify="mobile" readonly onfocus="this.removeAttribute('readonly');" onblur="this.setAttribute('readonly',true);" class="layui-input ns-len-long">
		</div>
		<div class="ns-word-aux">请输入正确的手机号</div>
	</div>
	
	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>密码：</label>
		<div class="layui-input-block">
			<input name="password" type="password" lay-verify="required" class="layui-input ns-len-long">
		</div>
		<div class="ns-word-aux">请输入登录密码</div>
	</div>
	
	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>再次输入密码：</label>
		<div class="layui-input-block">
			<input name="repassword" type="password" lay-verify="required|repassword" class="layui-input ns-len-long">
		</div>
		<div class="ns-word-aux">请输入登录密码，必须与第一次输入密码一致</div>
	</div>

	<div class="ns-form-row">
		<button class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
		<button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
	</div>
</div>
{/block}
{block name="script"}
<script>
	layui.use(['form'], function() {
		var form = layui.form,
			repeat_flag = false;//防重复标识
		form.render();

		/**
		 * 监听提交
		 */
		form.on('submit(save)', function(data) {
            if (repeat_flag) return;
            repeat_flag = true;
			
			$.ajax({
				type: 'POST',
				dataType: 'JSON',
				url: ns.url("admin/sysuser/add"),
				data: data.field,
				success: function(res){
					repeat_flag = false;
					if (res.code == 0) {
						layer.confirm('添加成功', {
							title:'操作提示',
							btn: ['返回列表', '继续添加'],
							closeBtn: 0,
							yes: function(){
								location.href = ns.url("admin/sysuser/lists")
							},
							btn2: function() {
								location.href = ns.url("admin/sysuser/add")
							}
						})
					}else{
						layer.msg(res.message);
					}
				}
			});
		});
		
		/**
		 * 表单验证
		 */
        form.verify({
            repassword: function (value) {
                if (value.length == 0) {
                    return '请输入密码!';
                }
                var pw = $("input[name='password']").val();
                if(value != pw){
                    return '两次密码输入不一致!';
				}
            },
			mobile: function(value) {
				var reg = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;
				if (value == '') {
					return;
				}
				if (!reg.test(value)) {
					return '请输入正确的手机号码!';
				}
			}

        });
	});
	
	function back() {
		location.href = ns.url("admin/sysuser/lists");
	}
</script>
{/block}